package com.oraycn.esbasic.helpers;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
 
/**
 * Created by ZY on 2019/7/23.
 */
public  class StringHelper {
    /**
    * MD5加密
    * @param string 加密前字符串
    * @return 加密后的字符串
    * */
    public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
    /**
    * 判断字符串是否为空或者长度为0
    * @param string
    * */
    public static boolean isNullOrEmpty(CharSequence string)
    {
        return string==null||string.length()==0;
    }

    public static String ContactString(Collection<String> objList, String contactor) {
        if (objList == null) {
            return null;
        }
        String result = "";
        for (String str : objList) {
            result += contactor + str;
        }
        if (result.length() > 0) {
            result = result.substring(1);
        }
        return result;
    }

    /**
     * 获取加密手机号码
     * */
    public static String getEncryptPhoneNumber(String phone)
    {

        if(StringHelper.isNullOrEmpty(phone))
        {
            return phone;
        }
        StringBuffer sb=new StringBuffer(phone);
        return sb.substring(0, 3) + "****" + sb.substring(7, sb.length());
    }

    /**
     * String左对齐
     * @param src 源字符串
     * @param len 总共长度
     * @param paddingChar 填充字符
     * @return
     */
    public static String padLeft(String src, int len, char paddingChar) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
        for (int i = src.length(); i < len; i++) {
            charr[i] = paddingChar;
        }
        return new String(charr);
    }

    /**
     * String右对齐
     * @param src 源字符串
     * @param len 总共长度
     * @param ch 填充字符
     * @return
     */
    public static String padRight(String src, int len, char ch) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
        for (int i = 0; i < diff; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }

    /**
     * 是否为符号
     * @param ch
     * @return
     */
    public static boolean isSymbol(char ch)
    {

        if(isCnSymbol(ch)) return true;

        if(isEnSymbol(ch))return true;

        if(0x2010 <= ch && ch <= 0x2017) return true;

        if(0x2020 <= ch && ch <= 0x2027) return true;

        if(0x2B00 <= ch && ch <= 0x2BFF) return true;

        if(0xFF03 <= ch && ch <= 0xFF06) return true;

        if(0xFF08 <= ch && ch <= 0xFF0B) return true;

        if(ch == 0xFF0D || ch == 0xFF0F) return true;

        if(0xFF1C <= ch && ch <= 0xFF1E) return true;

        if(ch == 0xFF20 || ch == 0xFF65) return true;

        if(0xFF3B <= ch && ch <= 0xFF40) return true;

        if(0xFF5B <= ch && ch <= 0xFF60) return true;

        if(ch == 0xFF62 || ch == 0xFF63) return true;

        if(ch == 0x0020 || ch == 0x3000) return true;

        return false;
    }

    static boolean isCnSymbol(char ch) {

        if (0x3004 <= ch && ch <= 0x301C) return true;

        if (0x3020 <= ch && ch <= 0x303F) return true;

        return false;

    }

    static boolean isEnSymbol(char ch){

        if (ch == 0x40) return true;

        if (ch == 0x2D || ch == 0x2F) return true;

        if (0x23 <= ch && ch <= 0x26) return true;

        if (0x28 <= ch && ch <= 0x2B) return true;

        if (0x3C <= ch && ch <= 0x3E) return true;

        if (0x5B <= ch && ch <= 0x60) return true;

        if (0x7B <= ch && ch <= 0x7E) return true;

        return false;

    }

    public static byte[] getBytes(String str) throws UnsupportedEncodingException {
        return str.getBytes("UTF-8");
    }

    public static String getString(byte[] data) throws UnsupportedEncodingException {
        return new String(data,"UTF-8");
    }
}
